import 'package:flutter/material.dart';

class NotebookPage extends StatefulWidget {
  const NotebookPage({Key? key}) : super(key: key);

  @override
  _NotebookPageState createState() => _NotebookPageState();
}

class _NotebookPageState extends State<NotebookPage> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();
  
  // 配置行高和横线样式
  final double _lineHeight = 32.0;
  final double _marginLeft = 40.0;
  final Color _lineColor = Colors.grey.withOpacity(0.3);
  final Color _paperColor = Colors.yellow[50]!;
  
  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('笔记本'),
        actions: [
          IconButton(
            icon: const Icon(Icons.save),
            onPressed: () => _saveNote(),
          ),
        ],
      ),
      body: Container(
        decoration: BoxDecoration(
          color: _paperColor,
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 2,
              blurRadius: 5,
              offset: const Offset(0, 3),
            ),
          ],
        ),
        child: Stack(
          children: [
            // 背景横线和装订线
            CustomPaint(
              size: Size.infinite,
              painter: NotebookPainter(
                lineHeight: _lineHeight,
                lineColor: _lineColor,
                marginLeft: _marginLeft,
              ),
            ),
            // 文本输入区域
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: TextField(
                controller: _controller,
                focusNode: _focusNode,
                maxLines: null,
                style: TextStyle(
                  fontSize: 18.0,
                  height: _lineHeight / 20.0, // 调整行高匹配横线间距
                ),
                decoration: const InputDecoration(
                  border: InputBorder.none,
                  hintText: '开始输入...',
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
  
  void _saveNote() {
    // 实现笔记保存逻辑
    final String text = _controller.text;
    if (text.isNotEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('笔记已保存')),
      );
    }
  }
}

class NotebookPainter extends CustomPainter {
  final double lineHeight;
  final Color lineColor;
  final double marginLeft;
  
  NotebookPainter({
    required this.lineHeight,
    required this.lineColor,
    required this.marginLeft,
  });
  
  @override
  void paint(Canvas canvas, Size size) {
    final Paint linePaint = Paint()
      ..color = lineColor
      ..strokeWidth = 1.0;
    
    // 计算行数
    final int lineCount = (size.height / lineHeight).ceil();
    
    // 绘制横线
    for (int i = 0; i < lineCount; i++) {
      final double y = lineHeight * (i + 1);
      canvas.drawLine(
        Offset(marginLeft, y),
        Offset(size.width, y),
        linePaint,
      );
    }
    
    // 绘制装订线
    final Paint marginPaint = Paint()
      ..color = Colors.grey.withOpacity(0.5)
      ..strokeWidth = 2.0;
    
    canvas.drawLine(
      Offset(marginLeft / 2, 0),
      Offset(marginLeft / 2, size.height),
      marginPaint,
    );
    
    // 绘制顶部阴影
    final Paint shadowPaint = Paint()
      ..color = Colors.black.withOpacity(0.1)
      ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3.0);
    
    canvas.drawRect(
      Rect.fromLTRB(0, 0, size.width, 5),
      shadowPaint,
    );
  }
  
  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}